package com.bw.paper.mvp.view.Fragment;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.bw.paper.mvp.view.Activity.LoginActivity;
import com.bw.paper.mvp.view.Activity.MoreActivity;
import com.bw.paper.mvp.view.Activity.UpActivity;
import com.bw.paper.mvp.view.Activity.UpUserActivity;
import com.bw.paper.mvp.view.Fragment.user.Adapter.UserAdapter;
import com.bw.paper.mvp.view.Fragment.user.Adapter.UserMenuAdapter;
import com.bw.paper.App;
import com.bw.paper.R;
import com.bw.paper.entity.BroadcastEntity;
import com.bw.paper.entity.UserEntity;
import com.bw.paper.mvp.base.model.BaseEntity;
import com.bw.paper.mvp.base.view.BaseFragment;
import com.bw.paper.mvp.contact.UserContract;
import com.bw.paper.mvp.di.DaggerUserComponent;
import com.bw.paper.mvp.di.UserModules;
import com.bw.paper.entity.UserItemEntity;
import com.bw.paper.entity.UserMenuEntity;
//import com.bw.paper.entity.Y_Dividers;
import com.bw.paper.mvp.presenter.UserPresenter;
import com.bw.paper.utils.ImgUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.NonNull;

public class MineFragment extends BaseFragment<UserPresenter> implements UserContract.IUserhView, OnItemChildClickListener {

    //wode我的页面
    public MineFragment() {
        // Required empty public constructor
    }

    private View menuView, timeLineView;
    private RecyclerView menu_rv;
    private UserMenuAdapter menuAdapter;
    private static TextView user_name;
    private static ImageView menu_head_img;
    private RecyclerView user_rv;
    private UserAdapter userAdapter;
    private UserBroadcast userBroadcast;

    private UpUserActivity.UserBroadcast broadcast;
    private List<UserItemEntity> itemes1 = new ArrayList<>();

    @Override
    public int bindlayout() {
        return R.layout.fragment_mine;
    }

    @Override
    public void onDestroy() {
        Log.e("asd", "---------------");
        super.onDestroy();
//       getActivity(). unregisterReceiver(broadcast);
    }


    public class UserBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BroadcastEntity.UPDATENAME)) {
                String nickname = intent.getStringExtra("username");
                user_name.setText(nickname);//接受传过来的值

                List<UserEntity> userEntities = App.getInstance().getDaoSession().getUserEntityDao().loadAll();
                for (UserEntity userEntity : userEntities) {
                    String user_name1 = userEntity.getUser_name();
                    user_name.setText(user_name1);
                    Log.e("====", "onReceive: " + user_name);
                }

            }
        }
    }

    @Override
    public void initView() {
        Log.e("asd", "========================");
        List<UserEntity> userEntities = App.getInstance().getDaoSession().loadAll(UserEntity.class);
        LogUtils.json("asd", userEntities);
        user_rv = f(R.id.user_rv);
        menuView = LayoutInflater.from(getContext()).inflate(R.layout.view_user_menu, null);

        timeLineView = LayoutInflater.from(getContext()).inflate(R.layout.view_user_speed, null);

        menu_rv = menuView.findViewById(R.id.menu_rv);
//        名字
        user_name = menuView.findViewById(R.id.user_name);
//        touxiang
        menu_head_img = menuView.findViewById(R.id.menu_head_img);


        user_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startPage(UpUserActivity.class);
            }
        });
        //        广播
        userBroadcast = new UserBroadcast();
        IntentFilter filter = new IntentFilter();
        filter.addAction(BroadcastEntity.UPDATENAME);
        getActivity().registerReceiver(userBroadcast, filter);


        menu_head_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    List<UserEntity> list = App.getInstance().getDaoSession().loadAll(UserEntity.class);
                    if (list.size() == 0) {
                        startPage(LoginActivity.class);
//                        getActivity().finish();
                    } else {
                        startPage(UpActivity.class);

//                        getActivity().finish();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

//                startPage(LoginActivity.class);
            }
        });


        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        menu_rv.setLayoutManager(gridLayoutManager);
        menuAdapter = new UserMenuAdapter();
        menu_rv.setAdapter(menuAdapter);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        user_rv.setLayoutManager(linearLayoutManager);

        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        user_rv.addItemDecoration(dividerItemDecoration);

        userAdapter = new UserAdapter();


        userAdapter.addHeaderView(menuView);
        userAdapter.addHeaderView(timeLineView);
        user_rv.setAdapter(userAdapter);


    }

    @Override
    public void inject() {
        DaggerUserComponent.builder().userModules(new UserModules(this)).build()
                .injectUser(this);

    }

    @Override
    public void initData() {
        Log.e("=========", "initData: " + 123);
        p.iniData();


    }


    @Override
    public void onStart() {
        super.onStart();
        try {
            List<UserEntity> list = App.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
            String user_img = list.get(0).getUser_img();
            LogUtils.json("img" + user_img);
            ImgUtils.getInstance().loadCircleImage(getContext(), user_img, menu_head_img);
            user_name.setText(list.get(0).getUser_name());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void refresh(BaseEntity baseEntity) {

    }

    @Override
    public void refreshMenu(List<UserMenuEntity> menues) {
        menuAdapter.setNewInstance(menues);

    }

    @Override
    public void refreshItemes(List<UserItemEntity> itemes) {
        userAdapter.setNewInstance(itemes);

        itemes1.addAll(itemes);
        userAdapter.setOnItemChildClickListener(this::onItemChildClick);


    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {

            case R.id.user_items_arrow_img:
                if (itemes1.get(position).name.equals("夜间模式")) {
                    boolean selFlag = userAdapter.getData().get(position).selFlag;
                    WindowManager.LayoutParams attributes = getActivity().getWindow().getAttributes();

                    if (selFlag) {
                        itemes1.get(position).setSelFlag(false);
                        userAdapter.setNewInstance(itemes1);
                        userAdapter.notifyDataSetChanged();
                        attributes.alpha = 1f;
                        getActivity().getWindow().setAttributes(attributes);
                        Log.e("asd", "太阳");
                    } else {
                        itemes1.get(position).setSelFlag(true);
                        userAdapter.setNewInstance(itemes1);
                        userAdapter.notifyDataSetChanged();
                        attributes.alpha = 0.5f;
                        getActivity().getWindow().setAttributes(attributes);
                        Log.e("asd", "月亮");
                    }
                    break;

                }
                if (itemes1.get(position).name.equals("更多设置")){
                    startPage(MoreActivity.class);
                }

        }

    }

}